package personnel.department.pages;

import static personnel.department.pages.constants.PageGroup.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import personnel.department.pages.constants.Icons;
import personnel.department.pages.constants.PageGroup;

public class PersonnelTree {

	private final Shell shell;
	private final Composite parent;
	private final Tree tree;
	private Composite rightComposite;
	private PagesLogic pagesLogic;

	public PersonnelTree(Shell shell, Composite parent, Composite rightComposite) {
		this.shell = shell;
		this.parent = parent;
		this.rightComposite = rightComposite;
		tree = new Tree(parent, SWT.FULL_SELECTION);
		pagesLogic = PagesLogic.getInstance();
	}

	public void createTree() {
		initTree();

		TreeItem findPerson = new TreeItem(tree, SWT.NONE);
		setPageGroup(findPerson, MEDIC_PERSONAL);
		findPerson.setImage(new Image(shell.getDisplay(), Icons.SEARCH_ICON.getPath()));

		TreeItem addPerson = new TreeItem(tree, SWT.NONE);
		setPageGroup(addPerson, ADD_PERSON);
		addPerson.setImage(new Image(shell.getDisplay(), Icons.ADD_ICON.getPath()));
		
		TreeItem queries = new TreeItem(tree, SWT.NONE);
		setPageGroup(queries, QUERIES);
		queries.setImage(new Image(shell.getDisplay(), Icons.QUERY_ICON.getPath()));
		
		tree.addListener(SWT.MouseDoubleClick, new Listener() {
			
			@Override
			public void handleEvent(Event e) {
				TreeItem[] items = tree.getSelection();
				for (TreeItem item : items) {
					pagesLogic.createPage((PageGroup)item.getData());
				}
				
			}
		});
	}

	private void setPageGroup(TreeItem item, PageGroup group) {
		item.setData(group);
		item.setText(group.getGroup());
	}

	private void initTree() {
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	}

	public Tree getTree() {
		return tree;
	}
}
